﻿using System;
using System.Linq;
using System.Web;

namespace HopNguyenModel.General
{
    public class GenerateMeta
    {
        /// <summary>
        /// 1 : Download video
        /// 2 : Watch video
        /// 3 : Page
        /// 4 : Search
        /// </summary>

        public static string AutoGenerateMeta(int type = 1, Content content = null, string title = "", string description = "", string alias = "", 
            string page = "", string query = "")
        {
            var newDescription = "";
            var image = "http://i3.ytimg.com/vi/" + alias + "/0.jpg";
            switch (type)
            {
                case 1: //Download video
                    var newTitle = "Download video: " + title;
                    newDescription = "Click to download video '" + title + "' with multiple formats 3gp, flv, mp4, full HD.";
                    var keywork = new HopNguyenEntities().Database.SqlQuery<string>("select MetaKeyword from PageDetail").FirstOrDefault();
                    return Meta(newTitle, newDescription, keywork, image);

                case 2: //Watch video
                    newDescription = "Click to watch and download video: " + content.Name + " and download in multiple formats 3gp, flv, mp4, Full HD";
                    return Meta(content.Name, newDescription, content.MetaKeyword, content.ImageShare, true, content.Alias);

                case 3: //Page
                    var db = new HopNguyenEntities();
                    var pageDetail = string.IsNullOrEmpty(page) ? db.PageDetails.FirstOrDefault() : db.PageDetails.FirstOrDefault(a => a.Alias == page);
                    pageDetail = pageDetail ?? new PageDetail();
                    return Meta(pageDetail.MetaTitle, pageDetail.MetaDescription, pageDetail.MetaKeyword);

                case 4: //Search
                    newDescription = "Search result for: " + query;
                    return Meta(newDescription, newDescription);

            }
            return null;
        }

        static string Meta(string title, string description,  string keyword = "",string image = "", bool watch = false, string alias = "")
        {
            image = string.IsNullOrEmpty(image) ? "http://www.getlinkyoutube.com/frontend/img/getlinkyoutube.jpg" : image;
            var url = HttpContext.Current.Request.Url;
            var meta = "<title>" + title + "</title>" + Environment.NewLine;
            meta += "    <meta name=\"title\" content=\"" + title + "\" />" + Environment.NewLine;
            if (!string.IsNullOrEmpty(keyword))
            {
                meta += "    <meta name=\"keywords\" content=\"" + keyword + "\" />" + Environment.NewLine;
            }
            if (!string.IsNullOrEmpty(description))
            {
                meta += "    <meta name=\"description\" content=\"" + description + "\" />" + Environment.NewLine + Environment.NewLine;
            }

            // Schema.org markup 
            meta += "    <meta itemprop=\"name\" content=\"" + title + "\" />" + Environment.NewLine;
            meta += "    <meta itemprop=\"description\" content=\"" + description + "\" />" + Environment.NewLine;
            meta += "    <meta itemprop=\"image\" content=\"" + image + "\" />" + Environment.NewLine + Environment.NewLine;

            //Twitter Card data
            meta += "    <meta name=\"twitter:card\" content=\"summary\" />" + Environment.NewLine;
            meta += "    <meta name=\"twitter:site\" content=\"@getlinkyoutube\" />" + Environment.NewLine;
            meta += "    <meta name=\"twitter:title\" content=\"" + title + "\" />" + Environment.NewLine;
            meta += "    <meta name=\"twitter:description\" content=\"" + description + "\" />" + Environment.NewLine;
            meta += "    <meta name=\"twitter:url\" content=\"" + url + "\" />" + Environment.NewLine;
            meta += "    <meta name=\"twitter:creator\" content=\"@getlinkyoutube\" />" + Environment.NewLine;
            meta += "    <meta name=\"twitter:image:src\" content=\"" + image + "\" />" + Environment.NewLine;
            if (watch)
            {
                meta += "    <meta name=\"twitter:player\" content=\"https://www.youtube.com/embed/" + alias + "\" />" + Environment.NewLine;
                meta += "    <meta name=\"twitter:player:width\" content=\"600\" />" + Environment.NewLine;
                meta += "    <meta name=\"twitter:player:height\" content=\"350\" />" + Environment.NewLine;
            }
            meta += Environment.NewLine;

            //Open Graph data
            meta += "    <meta property=\"og:title\" content=\"" + title + "\" />" + Environment.NewLine;
            meta += "    <meta property=\"og:url\" content=\"" + url + "\" />" + Environment.NewLine;
            meta += "    <meta property=\"og:image\" content=\"" + image + "\" />" + Environment.NewLine;
            meta += "    <meta property=\"og:description\" content=\"" + description + "\" />" + Environment.NewLine;
            meta += "    <meta property=\"og:site_name\" content=\"Get link Youtube\" />" + Environment.NewLine;
            if (watch)
            {
                meta += "    <meta property=\"og:type\" content=\"video\" />" + Environment.NewLine;
                meta += "    <meta property=\"og:video:type\" content=\"text/html\" />" + Environment.NewLine;
                meta += "    <meta property=\"og:video:width\" content=\"600\" />" + Environment.NewLine;
                meta += "    <meta property=\"og:video:height\" content=\"350\" />" + Environment.NewLine;
                meta += "    <meta property=\"og:video:url\" content=\"http://www.youtube.com/v/" + alias + "?autohide=1&amp;version=3\" />" + Environment.NewLine;
                meta += "    <meta property=\"og:video:secure_url\" content=\"https://www.youtube.com/v/" + alias + "?autohide=1&amp;version=3\" />" + Environment.NewLine;
                meta += "    <meta property=\"og:video:type\" content=\"application/x-shockwave-flash\" />" + Environment.NewLine;
            }
            meta += "    <link rel=\"image_src\" href=\"" + image + "\" />" + Environment.NewLine;
            meta += "    <link rel=\"canonical\" href=\"" + url + "\" />";
            return meta;
        }
    }
}
